package com.tomecode.tools.maven.settings.model;

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

/**
 * Contains informations required to a proxy settings.
 * 
 * @author Tomas Frastia
 * 
 * @see http://www.tomecode.com or
 *      http://code.google.com/p/maven-settings-editor/
 * 
 */
public final class Proxy
{

  private String id;
  /**
   * Whether this proxy configuration is the active one.
   */
  private boolean active = true;
  /**
   * The proxy protocol.
   */
  private String protocol;
  /**
   * The proxy host.
   */
  private String host;
  /**
   * The proxy port
   */
  private String port;
  /**
   * The proxy user.
   */
  private String username;
  /**
   * The proxy password.
   */
  private String password;
  /**
   * The list of non-proxied hosts (delimited by |).
   */
  private final List<String> nonProxyHosts;

  public Proxy()
  {
    nonProxyHosts = new ArrayList<String>();
  }

  /**
   * Copy exits {@link Proxy} to new {@link Proxy}
   * 
   * @param proxy
   */
  public Proxy(Proxy proxy)
  {
    this();
    id = proxy.getId();
    active = proxy.isActive();
    host = proxy.getHost();
    nonProxyHosts.addAll(proxy.nonProxyHosts);
    password = proxy.getPassword();
    port = proxy.getPort();
    protocol = proxy.getProtocol();
    username = proxy.getUsername();
  }

  public final String getId()
  {
    return id;
  }

  public final void setId(String id)
  {
    this.id = id;
  }

  public final boolean isActive()
  {
    return active;
  }

  public final void setActive(boolean active)
  {
    this.active = active;
  }

  public final String getProtocol()
  {
    return protocol;
  }

  public final void setProtocol(String protocol)
  {
    this.protocol = protocol;
  }

  public final String getHost()
  {
    return host;
  }

  public final void setHost(String host)
  {
    this.host = host;
  }

  public final String getPort()
  {
    return port;
  }

  public final void setPort(String port)
  {
    this.port = port;
  }

  public final String getUsername()
  {
    return username;
  }

  public final void setUsername(String username)
  {
    this.username = username;
  }

  public final String getPassword()
  {
    return password;
  }

  public final void setPassword(String password)
  {
    this.password = password;
  }

  public final String asNonProxyHosts()
  {
    String hosts = "";
    ListIterator<String> e = nonProxyHosts.listIterator();
    while (e.hasNext()) {
      hosts += e.next();
      if (e.hasNext()) {
        hosts += " | ";
      }
    }
    return hosts;
  }

  public final List<String> getNonProxyHosts()
  {
    return nonProxyHosts;
  }

  public final void addNonProxyHost(String nonProxyHost)
  {
    nonProxyHosts.add(nonProxyHost);
  }

  public final void setNonProxyHosts(List<String> nonProxyHosts)
  {
    this.nonProxyHosts.clear();
    this.nonProxyHosts.addAll(nonProxyHosts);
  }
}
